function formatNumber(n) {
	const str = n.toString()
	return str[1] ? str : `0${str}`
}

export function formatTime(date) {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()

	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	const t1 = [year, month, day].map(formatNumber).join('-')
	const t2 = [hour, minute, second].map(formatNumber).join(':')

	return `${t1} ${t2}`
}
export function regPhone(phone) {
	var reg = /^1[3-9]\d{9}$/;

	return reg.test(phone)
}
/*校验支付密码*/
export function regpayPwd(pwd) {
	var reg = /^\d{6}$/;
	return reg.test(pwd)
}
/*校验登录密码*/
export function regPwd(pwd) {
	// var reg = /^\S{6,20}$/;
	var reg =
		/^[^\u4e00-\u9fa5|\s\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3008|\u3009|\u3010|\u3011|\u300e|\u300f|\u300c|\u300d|\ufe43|\ufe44|\u3014|\u3015|\u2026|\u2014|\uff5e|\ufe4f|\uffe5]{6,30}$/;
	return reg.test(pwd)
}
var toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var base64Pad = '=';
/*字符串转化base64*/
export function toBase64(data) {
	var result = '';
	var length = data.length;
	var i;
	// Convert every three bytes to 4 ascii characters.                                                 

	for (i = 0; i < (length - 2); i += 3) {
		result += toBase64Table[data.charCodeAt(i) >> 2];
		result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];
		result += toBase64Table[((data.charCodeAt(i + 1) & 0x0f) << 2) + (data.charCodeAt(i + 2) >> 6)];
		result += toBase64Table[data.charCodeAt(i + 2) & 0x3f];
	}

	// Convert the remaining 1 or 2 bytes, pad out to 4 characters.                                     

	if (length % 3) {
		i = length - (length % 3);
		result += toBase64Table[data.charCodeAt(i) >> 2];
		if ((length % 3) == 2) {
			result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];
			result += toBase64Table[(data.charCodeAt(i + 1) & 0x0f) << 2];
			result += base64Pad;
		} else {
			result += toBase64Table[(data.charCodeAt(i) & 0x03) << 4];
			result += base64Pad + base64Pad;
		}
	}
	return result;
}
/** Convert Base64 data to a string */
var toBinaryTable = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
	14,
	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
	37, 38, 39, 40,
	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
];

export function base64ToString(data) {
	var result = '';
	var leftbits = 0; // number of bits decoded, but yet to be appended  
	var leftdata = 0; // bits decoded, but yet to be appended 
	// Convert one by one.                                                                             
	for (var i = 0; i < data.length; i++) {
		var c = toBinaryTable[data.charCodeAt(i) & 0x7f];
		var padding = (data.charCodeAt(i) == base64Pad.charCodeAt(0));
		// Skip illegal characters and whitespace  
		if (c == -1) continue;
		// Collect data into leftdata, update bitcount  
		leftdata = (leftdata << 6) | c;
		leftbits += 6;
		// If we have 8 or more bits, append 8 bits to the result 
		if (leftbits >= 8) {
			leftbits -= 8;
			// Append if not padding. 
			if (!padding)
				result += String.fromCharCode((leftdata >> leftbits) & 0xff);
			leftdata &= (1 << leftbits) - 1;
		}
	}
	// If there are any bits left, the base64 string was corrupted                                      
	if (leftbits)
		throw Components.Exception('Corrupted base64 string');
	return result;
}
// export const basePath='https://www.edu113.zkxltech.com/platform-face/'
// export const fileUrl='https://www.edu113.zkxltech.com/platform-face'
export const basePath = 'https://edu.lincusedu.com/platform-face/'
export const fileUrl = 'https://edu.lincusedu.com/platform-face'
export function padZero(value) {
	return `000000000${value}`.slice(-10)
}
export function getAgeByBirthday(strDay) {
	strDay = strDay.slice(0, 10);
	var arrDay = strDay.split('-');
	var birthYear = +arrDay[0];
	var birthMonth = +arrDay[1];
	var birthDay = +arrDay[2];

	var now = new Date();
	var nowYear = now.getFullYear();
	var nowMonth = now.getMonth() + 1;
	var nowDay = now.getDate();

	var age = nowYear - birthYear;
	if (age === 0) {
		return 0;
	}
	if (age < 0) {
		return -1;
	}

	var monthDiff = nowMonth - birthMonth;
	if (monthDiff === 0) {
		var dayDiff = nowDay - birthDay;
		if (dayDiff < 0) {
			return age - 1;
		}
		return age;
	} else if (monthDiff < 0) {
		return age - 1;
	}
	return age;
}
// 1.处理日期格式
export function getDate(datestr) {
	let temp = datestr.split('-')
	let date = new Date(temp[0], temp[1] - 1, temp[2])
	return date
}
// 2.获取时间段内的所有日期 Spacer:间隔符
export function formatEveryDay(start, end, Spacer) {
	let weekArr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
	let dateList = []
	let startTime = getDate(start)
	let endTime = getDate(end)

	while ((endTime.getTime() - startTime.getTime()) >= 0) {
		let year = startTime.getFullYear()
		let month = startTime.getMonth() + 1 < 10 ? '0' + (startTime.getMonth() + 1) : startTime.getMonth() + 1
		let day = startTime.getDate().toString().length == 1 ? '0' + startTime.getDate() : startTime.getDate()
		let timeDay = startTime.getDay(startTime)
		// dateList.push({
		//   time: year + '-' + month + '-' + day,
		//   week: weekArr[startTime.getDay(startTime)],
		//   isWeekend: timeDay === 0 || timeDay === 6
		// })
		if (Spacer) {
			// 如果不需要间隔符则直接拼接返回
			if (Spacer === 'null') {
				dateList.push(year + month + day) // 如20220422,20220423
			} else {
				// 需要自定义间隔符
				dateList.push(year + Spacer + month + Spacer + day)
			}
		} else {
			dateList.push(year + '-' + month + '-' + day) // 2022-04-22,2022-04-23
		}
		startTime.setDate(startTime.getDate() + 1)
	}
	return dateList
}

export default {
	formatNumber,
	formatTime,
	regPhone,
	regpayPwd,
	toBase64,
	base64ToString,
	basePath,
	fileUrl,
	regPwd,
	padZero,
	getAgeByBirthday,
	getDate,
	formatEveryDay
}